package com.example.myapplication;

import com.example.myapplication.shortvideo.utils.GsonUtil;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * @author: rongfeng
 * @date: 2021/1/26
 * @description
 */
public class Leeks {
    volatile int index = 1;

    public static void main(String[] args) {
        Leeks leeks = new Leeks();
        leeks.next();
    }

    private void next() {
        if (index <= 146) {
            System.out.println("index " + index);
            String url = String.format("http://fund.eastmoney.com/Data/FundDataPortfolio_Interface.aspx?dt=14&mc=returnjson&ft=all&pn=20&pi=%s&sc=abbname&st=asc", index);
            index++;
            getDns(url);
        } else {
            fenxi();
        }
    }


    private void fenxi() {
        int day = 10 * 365;
        int money = 10;
        int maxMoney = 50;
        int profit = 300;
        List<List<String>> list = new ArrayList<>();
        int count = 0;
        for (List<List<String>> item : mList) {
            for (List<String> str : item) {
                count++;
                int cDay = Integer.parseInt(str.get(6));
                int cMoney = 0;
                if (str.get(10).contains("亿元")) {
                    cMoney = (int) Float.parseFloat(str.get(10).replace("亿元", ""));
                }
                int cProfit = 0;
                try {
                    if (str.get(11).contains(".")) {
                        cProfit = Integer.parseInt(str.get(11).substring(0, str.get(11).indexOf(".")));
                    } else {
                        cProfit = Integer.parseInt(str.get(11).replace("%", ""));
                    }
                } catch (Exception e) {

                }
                if (cDay >= day && cMoney >= money && cMoney <= maxMoney && cProfit >= profit) {
                    str.add(cProfit + "");
                    list.add(str);
                }
            }
        }
        System.out.println("");
        System.out.println("共 " + count + " 条数据");
        System.out.println("符合条件 " + list.size() + " 条数据");
        System.out.println("\n");

        for (int i = 0; i < list.size(); i++) {
            for (int j = i + 1; j < list.size(); j++) {
                int p1 = Integer.parseInt(list.get(i).get(12));
                int p2 = Integer.parseInt(list.get(j).get(12));
                if (p2 > p1) {
                    list.add(i, list.get(j));
                    list.remove(j + 1);
                }
            }
        }

        for (List<String> strs : list) {
            System.out.println("姓名: " + strs.get(1));
            System.out.println("所属公司: " + strs.get(3));
            System.out.println("累计从业时间: " + (Integer.parseInt(strs.get(6)) / 365) + "年");
            System.out.println("现任基金资产总规模: " + strs.get(10));
            System.out.println("现任基金最佳回报: " + strs.get(11));
            System.out.println("现任基金: ");
            String[] names = strs.get(4).split(",");
            String[] code = strs.get(5).split(",");
            for (int i = 0; i < names.length; i++) {
                System.out.println(names[i] + " : " + code[i]);
            }
            System.out.println("\n\n");
        }
    }

    volatile List<List<List<String>>> mList = new ArrayList<>();


    public void getDns(String url) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .build();
        final Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, @NotNull okhttp3.Response response) throws IOException {
                if (response.body() != null) {
                    String html = response.body().string();
                    html = html.replace("var returnjson= {data:", "");
                    int index = html.lastIndexOf("]]");
                    html = html.substring(0, index + 2);
                    List<List<String>> bean = GsonUtil.parse(html, new TypeToken<List<List<String>>>() {
                    }.getType());
                    mList.add(bean);
                    next();
                }
            }
        });
    }


}
